package ${escapeKotlinIdentifiers(packageName)}.${escapeKotlinIdentifiers(fragmentPackage)}

import android.os.Bundle
import android.view.View
import androidx.lifecycle.ViewModelProviders
import androidx.recyclerview.widget.LinearLayoutManager
import cn.droidlover.xrecyclerview.RecyclerItemCallback
import ${escapeKotlinIdentifiers(packageName)}.BR
import ${escapeKotlinIdentifiers(packageName)}.R
import ${escapeKotlinIdentifiers(packageName)}.base.BaseVMFragment
import ${escapeKotlinIdentifiers(packageName)}.vm.ViewModelProviderFactory
import ${escapeKotlinIdentifiers(packageName)}.${escapeKotlinIdentifiers(viewModelPackage)}.${viewModelClass}
import ${escapeKotlinIdentifiers(packageName)}.databinding.${underscoreToCamelCase(classToResource(fragmentLayout))}Binding
import kotlinx.android.synthetic.main.${fragmentLayout}.*
import kotlinx.android.synthetic.main.title_bar.*
import javax.inject.Inject
import ${escapeKotlinIdentifiers(packageName)}.constants.Constants



/**
 * create by xuexuan
 * 来自薛瑄的自定义模板
 */

class ${fragmentClass} : BaseVMFragment<${underscoreToCamelCase(classToResource(fragmentLayout))}Binding>() {


    @Inject
    lateinit var factory: ViewModelProviderFactory
    override val bindingViewModel: Int get() = BR.viewModel
    override val bindingFragment: Int get() = BR.fragment
    override val layoutId: Int get() = R.layout.${fragmentLayout}
    override val viewModel: ${viewModelClass} get() = ViewModelProviders.of(this, factory).get(${viewModelClass}::class.java)

    private var mAdapter: ${adapterClass}? = null
    private var mPageId = Constants.START_PAGE_ID
    private var mXRecyclerView: XRecyclerView? = null


    override fun lazyInitView(view: View) {
        super.lazyInitView(view)
	
        toolBar.setNavigationOnClickListener { close() }
        title.text = ""
		
		mAdapter = ${adapterClass}(context!!){}

        initRecyclerView()
		observeData()
    }
	
	
	override fun initData(savedInstanceState: Bundle?) {
        super.initData(savedInstanceState)
        getData()
    }
	
	
	private fun initRecyclerView() {

        //数据列表的recycleView
        mXRecyclerView = recycler_content_layout.recyclerView
        mXRecyclerView?.useDefLoadMoreView()
		
		mAdapter?.recItemClick = object : RecyclerItemCallback<${modelClass}, ${adapterClass}.ViewHolder>() {
                    override fun onItemClick(
                        position: Int,
                        model: ${modelClass}?,
                        tag: Int,
                        holder: ${adapterClass}.ViewHolder?
                    ) {
                        super.onItemClick(position, model, tag, holder)
                        //点击事件处理
                        

                    }
                }
		
        mXRecyclerView?.setAdapter(mAdapter)
        mXRecyclerView?.verticalLayoutManager(context)

        mXRecyclerView?.onRefreshAndLoadMoreListener = object : XRecyclerView.OnRefreshAndLoadMoreListener {
            override fun onLoadMore(page: Int) {
                getData(page)
            }

            override fun onRefresh() {
			    getData()
            }
        }
    }
	


    private fun getData(pageId: Int?= Constants.START_PAGE_ID) {

        mPageId = pageId
        viewModel.getListByKey(pageId = pageId)
    }


    private fun observeData() {

        val observer = Observer<String> { showToast(it) }

        viewModel.mFailStringLD.observe(this,observer)
        viewModel.mSuccessStringLD.observe(this,observer)
        viewModel.mErrorLD.observe(this,Observer<NetError> { it ->
		
        })
        
        viewModel.mListLD.observe(this, Observer<PageBean<GiftListResponseBean>> { it ->
            //根据分页的数量和总数据是否为0 ，显示不同的界面
            mXRecyclerView?.setPage(it.current, it.pages)

            if (mPageId == Constants.START_PAGE_ID) {
                mAdapter?.setData(it.records)
            } else {
                mAdapter?.addData(it.records)
            }
        })

    }


    companion object {
        fun newInstance() = ${fragmentClass}()
    }


}